package com.acme.iolab.filecopy;

import com.acme.iolab.filecopy.io.IOFileCopyHelper;
import com.acme.iolab.filecopy.nio.NIOFileCopyHelper;

import java.io.File;
import java.io.IOException;

public abstract class FileCopyHelper {

    private static final String COPY_SOURCE = "F:\\FileFolder\\data";
    private static final String COPY_TARGET = "F:\\FileFolder\\data_cpn";

    public static void main(String[] args) throws IOException {
        FileCopyHelper helper;
        switch (args[0]) {
            case "-io":
                helper = new IOFileCopyHelper();
                break;
            case "-nio":
                helper = new NIOFileCopyHelper();
                break;
            default:
                System.out.println("Incorrect argument, required -io | -nio");
                return;
        }

        long start = System.currentTimeMillis();

        helper.copyFile(COPY_SOURCE, COPY_TARGET);

        System.out.println(System.currentTimeMillis() - start);
    }

    public final void copyFile(String source, String target) throws IOException {
        File sourceFile = new File(source);
        if (!sourceFile.exists()) {
            throw new IOException("Source file doesn't exists (" + source + ")");
        }
        File targetFile = new File(target);
        if (!targetFile.exists()) {
            if (!targetFile.createNewFile()) {
                throw new IOException("Can't create target file (" + target + ")");
            }
        }
        copyFile(sourceFile, targetFile);
    }

    protected abstract void copyFile(File source, File target) throws IOException;


}
